package com.xuecheng.media.service.jobhandler;

import com.xuecheng.base.utils.Mp4VideoUtil;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.service.MediaFileProcessService;
import com.xuecheng.media.service.MediaFileService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;

/**
 * @ClassName VideoTask
 * @Description 任务处理类
 * @Date 2023/6/23 14:53
 * @Version 1.0
 */
@Slf4j
@Component
public class VideoTask {

    @Autowired
    MediaFileService mediaFileService;

    @Autowired
    MediaFileProcessService mediaFileProcessService;

    @Value("${videoprocess.ffmpegpath}")
    String ffmpegPath;

    @Value("${minio.bucket.videofiles}")
    private String bucketVideos;

    //视频处理任务
    @XxlJob("videoJobHandler")
    public void videoJobHandler() throws Exception {

        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();//执行器的序号，从0开始
        int shardTotal = XxlJobHelper.getShardTotal();//执行器总数

        //确定cpu的核心数
        int processors = Runtime.getRuntime().availableProcessors();
        //查询待处理的任务
        List<MediaProcess> mediaProcessList = mediaFileProcessService.getMediaProcessList(shardIndex, shardTotal, processors);

        //任务数量
        int size = mediaProcessList.size();
        log.debug("取到视频处理任务数: " + size);
        if (size == 0) {
            return;
        }
        //创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(size);
        //使用计数器
        CountDownLatch countDownLatch = new CountDownLatch(size);
        mediaProcessList.forEach(mediaProcess -> {
            //将任务加入线程池
            executorService.execute(() -> {
                try {
                    //任务id
                    Long taskId = mediaProcess.getId();
                    //文件id就是md5
                    String fileId = mediaProcess.getFileId();
                    //开启任务
                    boolean b = mediaFileProcessService.startTask(taskId);
                    if (!b) {
                        log.debug("抢占任务失败,任务id:{}", taskId);
                        return;
                    }
                    //桶
                    String bucket = mediaProcess.getBucket();
                    //objectName
                    String objectName = mediaProcess.getFilePath();
                    //下载 minio 视频到本地
                    File file = mediaFileService.downloadFileFromMinIO(bucket, objectName);
                    if (file == null) {
                        log.debug("下载视频出错，任务id:{}，bucket:{}，objectName:{}", taskId, bucket, objectName);
                        //保存任务处理失败的结果
                        mediaFileProcessService.saveProcessFinishStatus(taskId, "3", fileId,
                                null, "下载视频到本地失败");
                        return;
                    }
                    //源 avi 视频的路径
                    String video_path = file.getAbsolutePath();
                    //转换后 mp4 文件的名称
                    String mp4_name = fileId + ".mp4";
                    //转换后 mp4 文件的路径
                    //先创建一个临时文件，作为转换后的文件
                    File mp4File = null;
                    try {
                        mp4File = File.createTempFile("minio", ".mp4");
                    } catch (IOException e) {
                        log.debug("创建临时文件异常, {}", e.getMessage());
                        //保存任务处理失败的结果
                        mediaFileProcessService.saveProcessFinishStatus(taskId, "3", fileId,
                                null, "创建临时文件异常");
                        return;
                    }
                    String mp4_path = mp4File.getAbsolutePath();
                    //创建工具类对象
                    Mp4VideoUtil videoUtil = new Mp4VideoUtil(ffmpegPath, video_path, mp4_name, mp4_path);
                    //开始视频转换，成功将返回 success，失败返回失败原因
                    String result = videoUtil.generateMp4();
                    if (!result.equals("success")) {
                        log.debug("视频转码失败，原因: {}，bucket: {}，objectName: {},", result, bucket, objectName);
                        mediaFileProcessService.saveProcessFinishStatus(taskId, "3", fileId, null, result);
                        return;
                    }
                    // mp4 文件的 url
                    String url = getFileUrl(fileId, ".mp4");
                    log.debug("taskId: {}, url: {}", taskId, url);
                    //将转码后的视频上传到 minio，这里的 objectName?
                    boolean b1 = mediaFileService.addMediaFilesToMinIO(mp4File.getAbsolutePath(),
                            "video/mp4", bucket, getMp4Path(fileId, ".mp4"));
                    if (!b1) {
                        log.debug("上传 mp4 到 minio 失败，taskId: {}", taskId);
                        mediaFileProcessService.saveProcessFinishStatus(taskId, "3", fileId,
                                null, "上传 mp4 到 minio 失败");
                        return;
                    }
                    //更新任务状态为成功
                    mediaFileProcessService.saveProcessFinishStatus(taskId, "2", fileId, url, "处理任务成功");
                } finally {
                    //计算器减去 1
                    countDownLatch.countDown();
                }
            });
        });
        //阻塞，指定最大限制的等待时间，阻塞最多等待一定的时间后就解除阻塞
        countDownLatch.await(30, TimeUnit.MINUTES);
    }

    private String getFileUrl(String fileMd5, String fileExt) {
        return  bucketVideos + "/" + fileMd5.charAt(0) + "/" + fileMd5.charAt(1) +
                "/" + fileMd5 + "/" + fileMd5 + fileExt;
    }

    private String getMp4Path(String fileMd5, String fileExt) {
        return fileMd5.charAt(0) + "/" + fileMd5.charAt(1) +
                "/" + fileMd5 + "/" + fileMd5 + fileExt;
    }

}

